﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using STT.Business;
using STT.Entities;
using STT.Layer.Connection;
using System.Data;
namespace STT.Admin.UserControls
{
    
    public partial class ucThemTKB : System.Web.UI.UserControl
    {
        ToolBox tb = new ToolBox();
        protected void Page_Load(object sender, EventArgs e)
        {

           

            if (!IsPostBack)
            {
                ddlKhoaHoc.DataSource = tb.GetDataTableByNguoiTao("TbKhoaHoc_SelectAll", Session["TenDN"].ToString());
                ddlKhoaHoc.DataTextField = "TenKhoaHoc";
                ddlKhoaHoc.DataValueField = "MaKhoaHoc";
                ddlKhoaHoc.DataBind();

                ddlKhoaHoc.Items.Insert(0, new ListItem { Text = "---------Chọn---------", Value = "-1" });
                ddlKhoaHoc.SelectedIndex = 0;

                Loanddl();

                //gvTKBview.DataSource = tb.GetDataTableByNguoiTao("TbTKB_SelectAll_View_UD", Session["TenDN"].ToString());
                //gvTKBview.DataBind();

            }



            if (Request.QueryString["idtkb"] != null)
            {
                int IdTKB = int.Parse(Request.QueryString["idtkb"]);

                // Lay thong tin thoi khoa bieu sua
                ETbTKB tkb = BTbTKB.SelectByID(IdTKB);

                ThongBao(tkb.NgayHoc.ToString());
                //ddlCaHoc. = tkb.CaHoc.ToString();
            }
        }

        void LoadTKB()
        {
            gvTKBview.AutoGenerateColumns = false;
            gvTKBview.DataSource = BTbTKB.SelectAllByIdClass(ddlLop.SelectedValue);
            gvTKBview.DataBind();
        }

        public void Loanddl()
        {
         ddlCaHoc.DataSource=BTbTKB.TbTKB_SelectAll_CaHoc();
         ddlGiangVien.DataSource = tb.GetDataTableByNguoiTao("TbGiangVien_SelectAll", Session["TenDN"].ToString());
         ddlMonHoc.DataSource = BTbMonHoc.SelectAll(Session["TenDN"].ToString());
            ddlPhongHoc.DataSource = BTbTKB.TbTKB_SelectAll_PhongHoc();


            ddlGiangVien.DataTextField="TenGV";
             ddlCaHoc.DataTextField="TenCa";
             ddlPhongHoc.DataTextField="TenPH";
             ddlMonHoc.DataTextField="TenMonHoc";
     
         
            ddlGiangVien.DataValueField="MaGV";
            ddlMonHoc.DataValueField="MaMonHoc";
            ddlPhongHoc.DataValueField="MaPH";
            ddlCaHoc.DataValueField="MaCa";

      
            ddlCaHoc.DataBind();
            ddlPhongHoc.DataBind();
            ddlMonHoc.DataBind();
            ddlGiangVien.DataBind();

            ddlCaHoc.Items.Insert(0, new ListItem { Text = "---------Chọn---------", Value = "-1" });
            ddlCaHoc.SelectedIndex = 0;

            ddlPhongHoc.Items.Insert(0, new ListItem { Text = "---------Chọn---------", Value = "-1" });
            ddlPhongHoc.SelectedIndex = 0;

            ddlMonHoc.Items.Insert(0, new ListItem { Text = "---------Chọn---------", Value = "-1" });
            ddlMonHoc.SelectedIndex = 0;

            ddlGiangVien.Items.Insert(0, new ListItem { Text = "---------Chọn---------", Value = "-1" });
            ddlGiangVien.SelectedIndex = 0;

        }

        

      

        protected void gvTKBview_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {

        }

     
      

 

       
        void ThongBao(string Loi)
        {
            ScriptManager.RegisterClientScriptBlock(this, GetType(), "Thông báo!", "<script>alert('" + Loi + "')</script>", false);
        }

        protected void lbXoaTat_Click(object sender, EventArgs e)
        {
            
        }

        protected void lbXuatDuLieu_Click1(object sender, EventArgs e)
        {

        }

        

        
    

        protected void btnThemMoi_Click1(object sender, EventArgs e)
        {


          

          
        }

        protected void btnLuu_Click(object sender, EventArgs e)
        {

        }
     
        protected void btnLuu_Click1(object sender, EventArgs e)
        {

            try
            {
                for (int i = 0; i < gvTKBview.Rows.Count; i++)
                {
                   

                    BTbTKB.Update_State_all(0);
                }

            }
            catch
            {
                ThongBao("Vui lòng kiểm tra lại dữ liệu!");
            }
        }
       
        protected void txtNgayHoc_TextChanged1(object sender, EventArgs e)
        {
            ddlGiangVien.Enabled = true;
            ddlKhoaHoc.Enabled = true;
            ddlMonHoc.Enabled = true;
            ddlCaHoc.Enabled = true;
            ddlPhongHoc.Enabled = true;
            btThemMoi.Enabled = true;
        }

        bool KiemTraDuLieu()
        {
            return BTbTKB.KiemTraDuLieu(new ETbTKB
            {
                     MaMonHoc = int.Parse(ddlMonHoc.SelectedValue),
                        MaGV = ddlGiangVien.SelectedValue,
                        NgayHoc = DateTime.Parse(txtNgayHoc.Text),
                        CaHoc = int.Parse(ddlCaHoc.SelectedValue),
                        PhongHoc = int.Parse(ddlPhongHoc.SelectedValue),
                        MaLop = int.Parse(ddlLop.SelectedValue),
                    

            });
        }
        protected void btThemMoi_Click(object sender, EventArgs e)
        {
           
                if (!KiemTraDuLieu())
                {

                    BTbTKB.Insert(new ETbTKB
                    {
                        MaMonHoc = int.Parse(ddlMonHoc.SelectedValue),
                        MaGV = ddlGiangVien.SelectedValue,
                        NgayHoc = DateTime.Parse(txtNgayHoc.Text),
                        CaHoc = int.Parse(ddlCaHoc.SelectedValue),
                        PhongHoc = int.Parse(ddlPhongHoc.SelectedValue),
                        MaLop = int.Parse(ddlLop.SelectedValue),
                        DiemDanh = 1
                    });
                }
            
                   
                

          
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            try
            {
             

                    BTbTKB.Update_State_all(0);
                

             
                gvTKBview.DataBind();
            }
            catch
            {
              
            }
        }
        public static String maTKB;
        protected void gvTKBview_RowCommand(object sender, GridViewCommandEventArgs e)
        {

        }

        protected void gvTKBview_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            try
            {
                int MaGV = int.Parse(gvTKBview.DataKeys[e.RowIndex].Value.ToString());

                BTbTKB.Delete(MaGV);
                Session["action"] = 3;
            }
            catch
            {
                ThongBao("Dữ liệu liên quan đến thời khóa biểu này vẫn tồn tại, vui lòng kiểm tra lại dữ liệu!");
            }
        }

        protected void ddKhoaHoc_SelectedIndexChanged(object sender, EventArgs e)
        {
           
                ddlLop.DataSource = BTbLop.SelectTuKhoaHoc(int.Parse(ddlKhoaHoc.SelectedValue));
                ddlLop.DataTextField = "TenLop";
                ddlLop.DataValueField = "MaLop";
                ddlLop.DataBind();

                ddlLop.Items.Insert(0, new ListItem { Text = "---------Chọn---------", Value = "-1" });
                ddlLop.SelectedIndex = 0;

        }

        protected void lbXoaTat_Click2(object sender, EventArgs e)
        {
            try
            {
               
                for (int i = 0; i < gvTKBview.Rows.Count; i++)
                {
                    CheckBox cbChon = (CheckBox)gvTKBview.Rows[i].Cells[0].FindControl("cbChon");
                    if (cbChon != null && cbChon.Checked)
                    {
                        BTbTKB.Update_State(new ETbTKB
                        {
                            MaTKB = int.Parse(gvTKBview.DataKeys[i].Values[0].ToString()),
                            DiemDanh = 2



                        });
                    }
                }

                Session["action"] = 3;

            }
            catch
            {
                ThongBao("Bạn chưa chọn học viên !");
            }
        }

        protected void btnImport_Click()
        {
           
        }

        protected void ddlLop_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadTKB();
        }
        
    }
}